﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoneImp {
	public class Skin {
		public string name;
		private Entry entries;
		
		public Skin(string name){
			this.name = name;
		}
		
		/* The Skin owns the attachment. */
		public void addAttachment(int slotIndex, string name, Attachment attachment) {
			Entry newEntry = new Entry(slotIndex, name, attachment);
			newEntry.next = this.entries;
			this.entries = newEntry;
		}

		/* Returns null if the attachment was not found. */
		public Attachment getAttachment(int slotIndex, string name) {
			Entry entry = this.entries;
			while (entry != null) {
				if (entry.slotIndex == slotIndex && entry.name == name) return entry.attachment;
				entry = entry.next;
			}
			return null;
		}

		/** Attach each attachment in this skin if the corresponding attachment in oldSkin is currently attached. */
		public void AttachAll(SkeletonBase skeleton, Skin oldSkin) {
			Entry entry = oldSkin.entries;
			while (entry != null) {
				Slot slot = skeleton.slots[entry.slotIndex];
				if (slot.attachment == entry.attachment) {
					Attachment attachment = this.getAttachment(entry.slotIndex, entry.name);
					if (attachment != null) slot.setAttachment(attachment);
				}
				entry = entry.next;
			}
		}

		/** Attach each attachment in this skin if the corresponding attachment in oldSkin is currently attached. */
		public void AttachAll(SkeletonBase skeleton) {
			Entry entry = entries;
			while (entry != null) {
				Slot slot = skeleton.slots[entry.slotIndex];
				Attachment attachment = this.getAttachment(entry.slotIndex, entry.name);
				if (attachment != null) slot.setAttachment(attachment);
				entry = entry.next;
			}
		}
	}
}
